home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Amos / AMOSList_0597 / AMOSLIST / text0063.txt < prev    next >
Encoding:
Text File  |  1998-06-24  |  4.2 KB  |  110 lines

  1.  
  2. I don't know if I saw it here or on a newsgroup, but I remember someone asking
  3. for a routine that maps the contents of a square bitmap into a trapezium (or
  4. trapezoid for the Americans out there).
  5.  
  6. I came across an old message by Andy Church in the list's archives that
  7. explains it all.
  8.  
  9. I hope it is appropriate to remail it here.
  10.  
  11. (stuff after the .sig best viewed with a non-proportional font).
  12.  
  13. -- 
  14. Branko Collin                                              . |. .
  15. collin@xs4all.nl                                         . . || ...
  16. http://www.xs4all.nl/~collin                          . ....||| .. ..
  17.  
  18. :From: achurch@dragon.mbhs.edu (Andy Church)
  19. :To: amos-list@access.digex.net
  20. :Subject: Re: 3D Mapping
  21. :Date: Sun Aug 13 01:44:54 1995
  22.  
  23. >Greetings one and all
  24. >
  25. >I want to map a picture of 320x320 pixels onto a polygon of horizontal
  26. >top and bottom edges, kinda like this:
  27. >
  28. >     X1_________________X2        Y1
  29. >      /                 \
  30. >     /                   \
  31. >    /                     \
  32. >   /                       \
  33. > X3~~~~~~~~~~~~~~~~~~~~~~~~~X4    Y2
  34. >
  35. >If you know the four x,y coords, (X1,Y1 X2,Y1 X3,Y2 X4,Y2) how can 
  36. >this be best achieved? I've tried for an hour or so, but I can't get 
  37. >it to work :( and I'm doing A level maths! I bet I'm missing 
  38. >something really obvious ;)
  39.  
  40.   Wow.  I remember this stuff from graphics class.  Real messy for the
  41. general case, at least as far as I got in it... I never actually finished
  42. implementing it.  I'll try to explain as best I can... but it's going to
  43. use some advanced math.  (Anyone else who has time and knows this, feel
  44. free to explain further, or correct me as necessary, or write up code.  I'm
  45. way too busy for any of that. <sigh>)
  46.  
  47.   First off, you have the mapping direction backwards.  Although it's
  48. counter-intuitive at first, you have to map *from* the polygon *to* the
  49. 320x320 bitmap.  This is so that you draw each pixel on the polygon exactly
  50. once.  If you go the other way, then you'll either draw the same pixel
  51. multiple times (if the polygon is smaller than the bitmap) or you'll have
  52. spaces between pixels (if the polygon is larger).
  53.  
  54.   Second, those have to be 3D coordinates, not 2D.  You'll need a Z
  55. coordinate to go with each of the Y coordinates.  I usually have Z increase
  56. going away from the viewer, with the screen being a plane of the form z=c (c
  57. is a constant).
  58.  
  59.   Anyway, this is the algorithm:
  60.  
  61. - Pick one corner of the polygon to be your origin.  (Let's use X1,Y1,Z1.)
  62.  
  63. - Let the two edges containing the origin be vectors p and q.  (I told you
  64. this was going to use advanced math...)  Keep in mind that these will be 3D
  65. vectors.  For this example, p is (X1,Y1,Z1)-(X2,Y1,Z1) and q is
  66.   (X1,Y1,Z1)-(X3,Y2,Z2).
  67.  
  68. - Let the two edges containing the matching corner of the bitmap be vectors p'
  69. and q'.  Ugh, that was a messy sentence.  Let me use a diagram:
  70.  
  71.        ________p________             __p'__
  72.       /.................\           |......|
  73.    q /.    .......       \          |..    |
  74.     /.            ......  \       q'|. .   |
  75.    /.                   ...\        |.  .  |
  76.    ~~~~~~~~~~~~~~~~~~~~~~~~~        |.   . |
  77.                                     |.    .|
  78.                                      ~~~~~~
  79. - For each point M:
  80.  
  81.     - Find values of a and b such that ap + bq = M.  For the general case,
  82. this gets tricky; if you know that one of the vectors is horizontal, though,
  83. it becomes easier.  Smething like this should work:
  84.  
  85. A#=((M(x)-X1)*1.0)/(X2-X1)
  86. B#=((M(y)-Y1)*1.0)/(Y2-Y1)
  87.  
  88.       That may look like it won't work because X3-X4 seems longer than
  89.       X1-X2.  But in 3D coordinates, those are actually the same size.
  90.       Those two lines are obviously not the most efficient way of doing
  91.       things; they're merely pseudocode.  (I use the M(x) notation to
  92.       mean "the x component of M".)
  93.  
  94.     - Find the corresponding point M' on the bitmap; M' = ap' + bq'.
  95.  
  96.     - Get the color of that pixel on the bitmap and plot it in the
  97.       polygon.
  98.  
  99.  
  100.   It's late at night, so I might have missed something, gotten something
  101. completely wrong, etc... again, anyone else who knows this stuff, just
  102. correct me as necessary. <yawn>
  103.  
  104.   --Andy Church (achurch@binx.mbhs.edu)
  105.     WWW: http://mmm.mbhs.edu/~achurch/
  106.     AMOS Web Site: http://mmm.mbhs.edu/~achurch/amos/
  107.  
  108.  
  109.  
  110.